package com.jing21.learn;

import org.springframework.context.annotation.Bean;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.ImplicitGrantBuilder;
import springfox.documentation.builders.OAuthBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.ApiKeyVehicle;
import springfox.documentation.swagger.web.SecurityConfiguration;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 郑靖 on 2017/9/27.
 */
//@Configuration
//@ConditionalOnProperty(value = "security.ignored", havingValue = "/common**")
//@EnableSwagger2
//@ConfigurationProperties("common")
//@Data
public class Swaager2Config {

    @PostConstruct
    public void successful() {

        System.err.println(this.toString());
        System.err.println("this is successful");
    }

    private String type = "oauth2";
    /**
     * 授权服务地址
     */
    private String authorizationUrl = "http://192.168.3.42:8830/se/oauth/authorize";
    /**
     * common 授权key
     */
    private String authClientId = "common";
    /**
     * common 授权Secret
     */
    private String authClientSecret = "common";
    /**
     *
     */
    private String scopeCode = "openid";
    /**
     * 描述
     */
    private String scopeDesc = "用户信息";
    private String appKey = "learn";
    private String appName = "学习应用";

    /**
     * 分组  key为group名 value为包名
     */
    private String group;

    private String pkg;

    @Bean
    public Docket docket() {
        List<SecurityScheme> securitySchemes = new ArrayList<>();
        securitySchemes.add(oauth());

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName(group)
                .genericModelSubstitutes(DeferredResult.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(false)
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage(pkg))
                .paths(PathSelectors.any())//过滤的接口
                .build()
                .securitySchemes(securitySchemes)
                .securityContexts(securityContexts())
                .apiInfo(DEFAULT_APIINFO);

    }


    List<SecurityContext> securityContexts() {
        List<SecurityContext> list = new ArrayList<>();
        list.add(securityContext());
        return list;
    }

    @Bean
    public SecurityConfiguration securityInfo() {
        return new SecurityConfiguration(authClientId, authClientSecret, scopeCode,
                appKey, appKey, ApiKeyVehicle.HEADER, "", ",");
    }

    @Bean
    SecurityScheme apiKey() {
        return new ApiKey(appName, appKey, "header");
    }


    @Bean
    SecurityContext securityContext() {
        AuthorizationScope[] scopes = new AuthorizationScope[]{new AuthorizationScope(scopeCode, scopeDesc)};

        SecurityReference securityReference = SecurityReference.builder().reference(type).scopes(scopes).build();

        List<SecurityReference> list = new ArrayList<>();
        list.add(securityReference);

        return SecurityContext.builder().securityReferences(list).forPaths(PathSelectors.any()).build();
    }


    @Bean
    SecurityScheme oauth() {
        return new OAuthBuilder()
                .name(type)
                .grantTypes(grantTypes())
                .scopes(scopes())
                .build();
    }

    List<AuthorizationScope> scopes() {

        List<AuthorizationScope> list = new ArrayList();
        list.add(new AuthorizationScope(scopeCode, scopeDesc));
        return list;
    }

    List<GrantType> grantTypes() {
        List<GrantType> grants = new ArrayList();
        GrantType grantType = new ImplicitGrantBuilder()
                .loginEndpoint(new LoginEndpoint(authorizationUrl))
                .build();
        grants.add(grantType);
        return grants;
    }

    private static final Contact DEFAULT_CONTACT = new Contact("zhengjing", "", "");

    private static final ApiInfo DEFAULT_APIINFO = new ApiInfo("Api Documentation", "Api Documentation", "1.0",
            "urn:tos",
            DEFAULT_CONTACT, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0",new ArrayList<>());

}
